<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();
    }
    public function register(){
        //添加用户
        //用户名是否存在，密码是否一致，口令加密，
        $userModel=D('Users');//生成一个模型对象，采用D方法才会自动验证
        if ($userModel->create()) {
            if ($_FILES['photo'][error] == 0) {//判断文件上传是否成功
                $fileName=$this->img();
                $userModel->photo=$fileName;
//				$photoinfo = $this->upload();//将临时文件保存到指定位置
//				$userModel->photo = $photoinfo['photo']['savename'];//获取文件并设置文件名称
            }
            if ($userModel->add()) {
                $this->success('注册成功', 'index');
            }else {
                $this->error('注册失败');
            }
        }else {
            $this->error($userModel->getError());
        }
    }
    public function upload(){//文件的转移
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
//		$upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $upload->autoSub=false;//不自动生成子文件夹
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            return $info;
        }
    }
    function img()//有图且上传成功，调用此方法
    {
        $photoinfo = $this->upload();
        $filePath='./Public/Uploads/photo/';
        $oldFileName=$photoinfo['photo']['savename'];
        $oldFileFullName=$filePath.'\\'.$oldFileName;
        $newFileName='thumb_'.$oldFileName;
        $newFileFullName=$filePath.'\\'.$newFileName;

        $image=new \Think\Image();
        $image->open($oldFileFullName);
        $image->thumb(100,100)->save($newFileFullName);
        return $newFileName;
    }
    public function  login(){
        $condition=array(
            'username'=>$_POST['username'],
            'password'=>md5(I('post.pass'))
        );
        $userModel=M('Users');
        $user=$userModel->where($condition)->find();
        if($user){
//            是合法用户,设置SESSION
            session('user',$user);
            $this->redirect('index.php/Admin/Index/users');
        }
        else{
//            非法用户
            $this->error('用户名或口令错误');
        }
    }
    public function  logout(){
        session('user',null);
//        exit();
        $this->redirect('index.php/Admin/Login/login');
    }
   public function about(){
        $newModel=M('News');
        $local_news=$newModel->order('id desc')->select();

        //select * from think_users order by id desc
        //$users 是函数中的局部变量，在外面不可见
        $this->news=$local_news;
        $this->display();
        //把当前对象的所以全局变量传递到对应的视图中，
        //可以使用assign方法限制传递的全局变量
    }
    public function about1(){
        $practiceModel=M('Practices');
        $local_practices=$practiceModel->order('id desc')->select();

        //select * from think_users order by id desc
        //$users 是函数中的局部变量，在外面不可见
        $this->practices=$local_practices;
        $this->display();
        //把当前对象的所以全局变量传递到对应的视图中，
        //可以使用assign方法限制传递的全局变量
    }
    public function addStupracs()
{

    $stupracModel=D('Stupracs');
    if( $stupracModel->create()){
        if($stupracModel->add()){
           $this->success('添加活动成功','');
        }
        else{
            $this->error('添加活动失败');
        }
     }else{
        $this->error($stupracModel->getError());
     }
   }
   public function addMembers()
{

    $memberModel=D('Members');
    if( $memberModel->create()){
        if($memberModel->add()){
           $this->success('加入社团成功','');
        }
        else{
            $this->error('加入社团失败');
        }
     }else{
        $this->error($memberModel->getError());
     }
   }
   function updateNewInfo($id){
        $newsModel=M('News');
        $this->new=$newsModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板updateUserInfo.html
    }
      function updatePracticeInfo($id){
        $practicesModel=M('Practices');
        $this->practice=$practicesModel->find($id);
        $this->display();
        //将查询到的用户信息传递给模板updateUserInfo.html
    }

    function updatePersonInfo(){
        $usersModel=M('Users');
        $this->user=$usersModel->order('uid desc')->limit(1)->find();
        $this->display();
        //将查询到的用户信息传递给模板updateUserInfo.html
    }
     function update(){
        $usersModel=M('Users');
        $this->user=$usersModel->order('uid desc')->limit(1)->find();
        $this->display();
        //将查询到的用户信息传递给模板updateUserInfo.html
    }

    function update0(){
       
        $userModel=D('Users');
        $userModel->create();
        if($_FILES['photo']['error']==0){
        $photoinfo=$this->upload();
        $userModel->photo=$photoinfo['photo']['savename'];
    }
        if($userModel->save()){
           $this->success('修改用户成功','users');
        }
        else{
            $this->error('修改用户失败');
        }
    }
    
 }
